package com.tjmc.recruit.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.pig4cloud.plugin.websocket.config.WebSocketMessageSender;
import com.pig4cloud.plugin.websocket.handler.PlanTextMessageHandler;
import com.tjmc.recruit.admin.api.entity.SessionInfoEntity;
import com.tjmc.recruit.admin.api.enums.ChatMessageTypeEnum;
import com.tjmc.recruit.admin.service.SessionInfoService;
import com.tjmc.recruit.finance.api.enums.ChangeMehtodEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class ChatPlanTextMessageHandler implements PlanTextMessageHandler {
    private final SessionInfoService sessionInfoService;

    @Override
    public void handle(WebSocketSession session, String message) {
        log.info("收到消息，会话ID:{},会话内容：{}", session.getId(), message);
        if (message == null) {
            return;
        }
        if (StrUtil.isNotBlank(message) && message.equals("PING")) {
            try {
                session.sendMessage(new TextMessage("PONG"));
                return;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        SessionInfoEntity sessionInfoEntity = null;
        try {
            sessionInfoEntity = JSONObject.parseObject(message, SessionInfoEntity.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (sessionInfoEntity == null) {
            log.error("会话解析失败");
            return;
        }
        if (StrUtil.isBlank(sessionInfoEntity.getReceiverUserName())) {
            log.error("会话接收方名称为空");
            return;
        }

        if (StrUtil.isBlank(sessionInfoEntity.getMessageContent())) {
            log.error("发送消息为空");
            return;
        }
        //保存消息
        sessionInfoService.saveOrUpdate(sessionInfoEntity);
        //将消息转发
        WebSocketMessageSender.send(sessionInfoEntity.getReceiverUserName(), JSONObject.toJSONString(sessionInfoEntity));
        //修改操作结果
        Long operationMessageId = sessionInfoEntity.getOperationMessageId();
        switch (sessionInfoEntity.getMessageType()) {
            //同意操作
            case EXCHANGE_PHONE_CONFIRM:
            case INTERVIEW_INVITE_CONFIRM:
            case RESUME_CHECK_CONFIRM:
            case EXCHANGE_WX_CONFIRM:
                if (operationMessageId != null) {
                    sessionInfoService.update(new LambdaUpdateWrapper<SessionInfoEntity>()
                            .eq(SessionInfoEntity::getId, operationMessageId)
                            .set(SessionInfoEntity::getOperationFlag, 1));
                }
                break;
            //拒绝操作
            case EXCHANGE_PHONE_REFUSE:
            case EXCHANGE_WX_REFUSE:
            case RESUME_CHECK_REFUSE:
            case INTERVIEW_INVITE_REFUSE:
                if (operationMessageId != null) {
                    sessionInfoService.update(new LambdaUpdateWrapper<SessionInfoEntity>()
                            .eq(SessionInfoEntity::getId, operationMessageId)
                            .set(SessionInfoEntity::getOperationFlag, 2));
                }
                break;
            default:
                break;
        }
    }
}
